package com.ruoyi.quartz.util;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;

public class ScrmSignTest {
    public static void main(String[] args) throws Exception {
        //secure_key
        String secure = "scrmmp_4410518d8dd7456c93a18c8593dbbe9e";
        System.out.println("===========secure=========== " + secure);
        Long timestamp = System.currentTimeMillis();
        System.out.println("===========timestamp=========== " + timestamp);
//        String urlParameters = "monitorType=SCRMMP&projectName=SCRM";
//        String urlParameters = "projectName=SCRM";
        String urlParameters = "";
        String stringToSign = timestamp+"";
        if (ObjectUtil.isNotEmpty(urlParameters)){
            stringToSign = stringToSign + "\n" + urlParameters ;
        }
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secure.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8));
        String sign = new String(Base64.encodeBase64(signData));
        System.out.println(sign);
    }
}